
package codeGenerate.powerdesigner.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import codeGenerate.common.utils.SystemUtils;
import codeGenerate.powerdesigner.converter.impl.DefaultPowerdesignerConverter;
import codeGenerate.powerdesigner.entry.PowerdesignerConfigVo;
import codeGenerate.powerdesigner.generater.PowerdesignerGenerater;

@SuppressWarnings("all")
public class PowerdesignerGenUtils {

	public static void genCode(String configPath) throws Exception {
		File file = new File(configPath);
		if (!file.exists()) {
			System.out.println("文件不存在：" + configPath);
		}
		FileInputStream is = new FileInputStream(file);
		genCode(is);
	}

	public static void genCode(InputStream is) throws Exception {
		List<PowerdesignerConfigVo> configList = readConfig(is);
		for (PowerdesignerConfigVo cf : configList) {
			PowerdesignerGenerater generater = new PowerdesignerGenerater();
			generater.setConfigVo(cf);
			generater.generateCode();
		}
	}

	public static List<PowerdesignerConfigVo> readConfig(InputStream is) throws IOException, DocumentException {
		try {

			List<PowerdesignerConfigVo> configVoList = new ArrayList<>();
			//创建解析器
			SAXReader reader = new SAXReader();
			//得到document
			Document document = reader.read(is);
			Element root = document.getRootElement();
			String genPath = root.elementText("genPath");
			String converter = StringUtils.defaultIfBlank(root.elementText("converter"), DefaultPowerdesignerConverter.class.getName());
			String excelTemplatePath = StringUtils.defaultIfBlank(root.elementText("excelTemplatePath"),
					SystemUtils.defalutTemplateDirectory() + File.separator + "powerdesigner" + File.separator + "tableTemplate.xlsx");
			Element beansEle = root.element("beans");
			List<Element> beanEles = beansEle.elements("bean");
			for (Element beanEle : beanEles) {
				PowerdesignerConfigVo cv = new PowerdesignerConfigVo();
				cv.setGenPath(genPath);
				cv.setFieldContent(beanEle.getText());
				cv.setTableCode(beanEle.attributeValue("tableCode"));
				cv.setTableComment(beanEle.attributeValue("tableComment"));
				cv.setConverter(converter);
				cv.setExcelTemplatePath(excelTemplatePath);
				configVoList.add(cv);
			}

			//验证
			for (PowerdesignerConfigVo configVo : configVoList) {
				if (StringUtils.isBlank(configVo.getFieldContent())) {
					throw new RuntimeException("字段内容不能为空");
				}
				if (StringUtils.isBlank(configVo.getGenPath())) {
					throw new RuntimeException("生成路径不能为空");
				}
				if (StringUtils.isBlank(configVo.getTableCode())) {
					throw new RuntimeException("表编码不能为空");
				}
				if (StringUtils.isBlank(configVo.getTableComment())) {
					throw new RuntimeException("表注解不能为空");
				}
			}
			return configVoList;
		} finally {
			is.close();
		}

	}

}
